package com.zb.framework.controller;

import com.zb.framework.api.dto.DemoDTO;
import com.zb.framework.api.microservice.DemoMicroService;
import com.zb.framework.api.microservice.DubboMicroService;
import com.zb.framework.model.LdapOu;
import com.zb.framework.service.impl.ActiveDomainServiceImpl;
import com.zb.zeus.common.core.exception.BaseException;
import com.zb.zeus.common.core.web.controller.BaseAction;
import com.zb.zeus.common.core.web.domain.AjaxResult;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.URLDecoder;
import java.util.Collections;
import java.util.concurrent.TimeUnit;

/**
 * @author fujianjian
 * @since 2021/7/1 22:07
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/demo")
@Api(value = "DEMO服务接口", tags = "DEMO服务接口")
public class DemoController extends BaseAction {

    private final Environment environment;

    @DubboReference(group = "default", version = "1.0")
    private DubboMicroService dubboMicroService;

    private final DemoMicroService demoMicroService;


    @GetMapping("/detail")
    public AjaxResult detail(DemoDTO reqDto) {
        log.info("....>>>>sssssssssssssssssssss");

        try {
            TimeUnit.MILLISECONDS.sleep(1);
        } catch (InterruptedException e) {
            log.error("ee", e);
        }
        // DemoModel model = demoService.getOne(Wrappers.query(reqDto));
        AjaxResult ret = demoMicroService.greet(environment.getProperty("spring.application.name"));
        log.info("feign client call greet ret:{}", ret);
        AjaxResult result = new AjaxResult(200, "success", Collections.EMPTY_MAP);
        return result;
    }

    @GetMapping("/greet")
    public AjaxResult greet(String source) {
        try {
            log.info("testsssssssssssssssssssss,{}", source);
            dubboMicroService.hello(source);
            AjaxResult ret = AjaxResult.success(environment.getProperty("spring.application.name"));
            return ret;
        } catch (Exception e) {
            log.error("error", e);
            throw new BaseException("error", e);
        }

    }

    @Autowired(required = false)
    private ActiveDomainServiceImpl activeDomainService;

    @GetMapping("/syncAdUser")
    public AjaxResult syncAdUser(String source) {
        try {
            log.info("testsssssssssssssssssssss,{}", source);
            activeDomainService.addWhaleLdapUser();
            AjaxResult ret = AjaxResult.success(environment.getProperty("spring.application.name"));
            return ret;
        } catch (Exception e) {
            log.error("error", e);
            throw new BaseException("error", e);
        }
    }

    @GetMapping("/syncAdOu")
    public AjaxResult syncAdOu(String source) {
        try {
            log.info("oooou,{}", source);
            activeDomainService.addWhaleLdapOu();
            AjaxResult ret = AjaxResult.success(environment.getProperty("spring.application.name"));
            return ret;
        } catch (Exception e) {
            log.error("error", e);
            throw new BaseException("error", e);
        }
    }

    @GetMapping("/setAdUser")
    public AjaxResult setAdUser(String source) {
        try {
            log.info("setooou,{}", source);
            // activeDomainService.setPwdWhaleLdapUser();
            activeDomainService.setPwdShanlinLdapUser();
            AjaxResult ret = AjaxResult.success(environment.getProperty("spring.application.name"));
            return ret;
        } catch (Exception e) {
            log.error("error", e);
            throw new BaseException("error", e);
        }
    }

    @GetMapping("/modifyAdOu")
    public AjaxResult modifyAdOrg(String disting) {
        try {
            disting = URLDecoder.decode(disting);
            log.info("setooou,{}", disting);
            activeDomainService.modifyOU(disting);
            AjaxResult ret = AjaxResult.success(environment.getProperty("spring.application.name"));
            return ret;
        } catch (Exception e) {
            log.error("error", e);
            throw new BaseException("error", e);
        }
    }

    @PostMapping("/addSingleOu")
    public AjaxResult addSingleOu(@RequestBody LdapOu ldapOu) {

        try {
            activeDomainService.afterCopySet(ldapOu);
            activeDomainService.adOu(ldapOu);
            AjaxResult ret = AjaxResult.success(environment.getProperty("spring.application.name"));
            return ret;
        } catch (Exception e) {
            log.error("error", e);
            throw new BaseException("error", e);
        }
    }

}
